package io.renren.modules.skate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.service.impl.BaseServiceImpl;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.constant.Constant;
import io.renren.common.utils.ConvertUtils;
import io.renren.common.utils.DateUtils;
import io.renren.modules.security.password.PasswordUtils;
import io.renren.modules.skate.dao.FClubDao;
import io.renren.modules.skate.dto.FClubDTO;
import io.renren.modules.skate.entity.FClubEntity;
import io.renren.modules.skate.service.FClubService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.sys.dao.SysRoleUserDao;
import io.renren.modules.sys.dao.SysUserDao;
import io.renren.modules.sys.entity.SysRoleUserEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.swagger.models.auth.In;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 俱乐部表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 3.0 2023-11-28
 */
@Service
public class FClubServiceImpl extends CrudServiceImpl<FClubDao, FClubEntity, FClubDTO> implements FClubService {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysRoleUserDao sysRoleUserDao;

    @Override
    public QueryWrapper<FClubEntity> getWrapper(Map<String, Object> params){
        QueryWrapper<FClubEntity> wrapper = new QueryWrapper<>();
        Integer id = (Integer)params.get("id");
        if(id != null){
            wrapper.eq("id", id);
        }
        return wrapper;
    }

    @Override
    public FClubDTO queryByName(String clubName) {
        FClubEntity fClubEntity = baseDao.selectOne(new QueryWrapper<FClubEntity>().eq("club_name", clubName).last("limit 1"));
        return ConvertUtils.sourceToTarget(fClubEntity, currentDtoClass());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveClub(FClubEntity fClubEntity) {
        BaseServiceImpl.retBool(baseDao.insert(fClubEntity));
        SysUserEntity sysUserEntity = new SysUserEntity();
        //加密
        String password = fClubEntity.getPassword();
        if(StringUtils.isBlank(password)){
            password = "123456";
        }
        sysUserEntity.setUsername(fClubEntity.getPhone());
        sysUserEntity.setPassword(PasswordUtils.encode(password));
        sysUserEntity.setRealName(fClubEntity.getUsername());
        sysUserEntity.setMobile(fClubEntity.getPhone());
        //俱乐部管理员
        sysUserEntity.setSuperAdmin(0);
        //正常
        sysUserEntity.setStatus(1);
        sysUserEntity.setClubId(fClubEntity.getId().intValue());
        BaseServiceImpl.retBool(sysUserDao.insert(sysUserEntity));
        //保存用户与角色关系
        SysRoleUserEntity sysRoleUserEntity = new SysRoleUserEntity();
        sysRoleUserEntity.setUserId(sysUserEntity.getId());
        //俱乐部角色
        sysRoleUserEntity.setRoleId(Constant.CLUBROLEID);
        sysRoleUserDao.insert(sysRoleUserEntity);
    }

    // 删除俱乐部
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteClub(Long[] ids) {
        for (Long id : ids) {
            // 删除俱乐部
            baseDao.deleteById(id);
            // 用户
            SysUserEntity sysUserEntity = sysUserDao.selectOne(new QueryWrapper<SysUserEntity>().eq("club_id", id));
            // 删除俱乐部管理员
            sysUserDao.delete(new QueryWrapper<SysUserEntity>().eq("club_id", id));
            // 删除俱乐部管理员角色
            if(sysUserEntity != null) {
                sysRoleUserDao.delete(new QueryWrapper<SysRoleUserEntity>().eq("user_id", sysUserEntity.getId()));
            }
        }
    }

    @Override
    public List<FClubDTO> quryClubList() {
        List<FClubEntity> list = baseDao.selectList(null);

        return ConvertUtils.sourceToTarget(list, currentDtoClass());
    }
}